package club.emergency.wechat.department.search.provider.impl;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.department.search.DepartmentSearch;
import club.emergency.wechat.model.department.Department;
import club.emergency.wechat.persistence.department.DepartmentMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.department.search.provider.impl
 * Date: 2019/3/23
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@RestController
public class DepartmentSearchImpl implements DepartmentSearch {

    private DepartmentMapper departmentMapper;

    public DepartmentSearchImpl(DepartmentMapper departmentMapper) {
        this.departmentMapper = departmentMapper;
    }

    @Override
    public String flipList(
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.likeRight(Objects.nonNull(code), "code", code);
        Page<Department> page = new Page<>(pageNo, pageSize);
        return ResponseEntity.ok(StatusCode.DB_SAVE_OK, "查询企业数据成功！", departmentMapper.selectPage(page, queryWrapper));
    }
}
